package cn.springboot.erp.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.springboot.erp.annotation.Log;
import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Customers;
import cn.springboot.erp.enums.Operation;
import cn.springboot.erp.exception.CustomerException;
import cn.springboot.erp.service.CustomersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("customers")
@Api("客户管理")
public class CustomersController {

    @Resource
    private CustomersService customersService;


    @RequiresPermissions("base:customers:select")
    @Log(description = Operation.SELECT,model = "客户管理")
    @ApiOperation("客户名称列表")
    @GetMapping("customerNameList")
    public ResponseResult customerNameList(){
        LambdaQueryWrapper<Customers> lambda = new QueryWrapper<Customers>().lambda();
        lambda.select(Customers::getId,Customers::getName);
        List<Customers> list = customersService.list(lambda);
        return ResponseResult.Success().data("customerNameList",list);
    }


    @RequiresPermissions("base:customers:select")
    @Log(description = Operation.SELECT,model = "客户管理")
    @ApiOperation("数据分页")
    @PostMapping("page")
    public ResponseResult listPage(@RequestBody QueryParams<Customers> queryParams){
        return customersService.listPage(queryParams);
    }

    @RequiresPermissions("base:customers:select")
    @Log(description = Operation.SELECT,model = "客户管理")
    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        Customers customers = customersService.getById(id);
        return ResponseResult.Success().data("customer",customers);
    }

    @RequiresPermissions(value = {"base:customers:update","base:customers:add"})
    @Log(description = Operation.ADDORUPDATE,model = "客户管理")
    @ApiOperation("添加或编辑")
    @PostMapping("insertOrUpdate")
    public ResponseResult insertOrUpdate(@RequestBody Customers customers) throws CustomerException {
        String name = customers.getName();
        LambdaQueryWrapper<Customers> lambda = new QueryWrapper<Customers>().lambda();
        lambda.eq(Customers::getName,name);
        Customers one = customersService.getOne(lambda);
        if (ObjectUtils.isNotNull(one)){
            if (ObjectUtil.equal(one.getId(),customers.getId())){
                customersService.saveOrUpdate(customers);
                return ResponseResult.Success().msg("编辑成功");
            }
            throw new CustomerException("该客户名称已经存在");
        }
        customersService.saveOrUpdate(customers);
        return ResponseResult.Success().msg("添加成功");
    }

    @RequiresPermissions("base:customers:delete")
    @Log(description = Operation.DELETE,model = "客户管理")
    @ApiOperation("根据id删除")
    @GetMapping("deleteById/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        customersService.removeById(id);
        return ResponseResult.Success().msg("删除成功");
    }

    @RequiresPermissions("base:customers:add")
    @Log(description = Operation.DELETE,model = "客户管理")
    @ApiOperation("批量导入")
    @PostMapping("import")
    public ResponseResult importCustomer(MultipartFile file) throws Exception {
        return customersService.importCustomer(file);
    }

    @RequiresPermissions("base:customers:select")
    @Log(description = Operation.DELETE,model = "客户管理")
    @ApiOperation("批量导出")
    @PostMapping("export")
    public void exportCustomer(HttpServletResponse response,@RequestBody Map<String,Object> params) throws IOException {
        customersService.exportCustomer(response,params);
    }
}
